<template>
	<view class="bianjiyaop">
		<headertop :title="pagetitle" str1="oridei" :back1="back1"></headertop>
		<view class="bianjiyaopback">

		</view>
		<view class="" :style="{'margin-top':statusBarHeight+'px','width':'100%','height':barHeight+'px'}">
		</view>
		<view class="centercontent">
			<view class="msgtext">
				<image :src="_static('/images/Frame@2x(32).png')" class="gantanhao" mode=""></image>
				<span>请遵医嘱选择用药剂量、频率、时间</span>
			</view>
			<view class="mouldff">
				<view class="line" @click="gopage">
					<view style="display: flex;align-items: center;">
						<span style="color: #F53F3F;">*</span>
						<span>药品名称：</span>
					</view>

					<view style="display: flex;align-items: center;">
						<span style="color:#86909C ;" v-if="!objnr['name']">请选择药品</span>
						<span style="width: 450rpx;text-align: right;" v-else>{{objnr['name']}}</span>
						<image :src="_static('/images/Frjt.png')" style="width: 24rpx;height: 24rpx;" mode="">
						</image>
					</view>
				</view>
				<view class="line" @click="open('药品剂量')" style="border: none;">
					<view style="display: flex;align-items: center;">
						<span style="color: #F53F3F;">*</span>
						<span>药品剂量：</span>
					</view>
					<view style="display: flex;align-items: center;">
						<span style="color:#86909C ;" v-if="!formData['dose']">请选择剂量</span>
						<span v-else>{{formData['dose']}}</span>
						<image :src="_static('/images/Frjt.png')" style="width: 24rpx;height: 24rpx;" mode="">
						</image>
					</view>
				</view>
			</view>
			<view class="mouldff" style="margin-top: 32rpx;">
				<view class="line" @click="xz(1)">
					<span>早饭后：</span>
					<image
						:src="bool(usage_timearr,1)?_static('/images/Group 11730@2x.png'):_static('/images/Ellipse 31@2x.png')"
						mode=""></image>
				</view>
				<view class="line" @click="xz(2)">
					<span>午饭后：</span>
					<image
						:src="bool(usage_timearr,2)?_static('/images/Group 11730@2x.png'):_static('/images/Ellipse 31@2x.png')"
						mode=""></image>
				</view>
				<view class="line" @click="xz(3)">
					<span>晚饭后：</span>
					<image
						:src="bool(usage_timearr,3)?_static('/images/Group 11730@2x.png'):_static('/images/Ellipse 31@2x.png')"
						mode=""></image>
				</view>
				<view class="line" style="border: none;" @click="xz(4)">
					<span>睡前：</span>
					<image
						:src="bool(usage_timearr,4)?_static('/images/Group 11730@2x.png'):_static('/images/Ellipse 31@2x.png')"
						mode=""></image>
				</view>
			</view>
		</view>
		<view class="fixed">
			<view class="center">
				<view class="savebtn" @click="save">
					保存
				</view>
			</view>
		</view>
		<view class="xzx">
			<up-popup :show="show" mode="bottom">
				<view class="back">
					<view class="xuanxiang">
						<view class="tt" style="margin-left: 30rpx;color: #888;" @click="show=false">
							取消
						</view>
						<view class="tt" style="font-weight: 600;">
							请选择剂量
						</view>
						<view class="tt" style="margin-right: 30rpx;color: #0077BE;" @click="queding">
							确认
						</view>
					</view>
					<picker-view immediate-change="true" :indicator-style="indicatorStyle" :value="value1"
						@change="bindChange" class="picker-view">
						<picker-view-column>
							<view class="item" v-for="(item,index) in  objnr['dose_list']" :key="index">
								{{item}}
							</view>
						</picker-view-column>
					</picker-view>
					<!--  -->
				</view>
			</up-popup>
		</view>
	</view>

</template>
<script setup>
	import headertop from '@/components/header.vue'
	import {
		_static
	} from '@/ulm/utils/url.js'
	import ulm, {
		$api
	} from '@/ulm';
	import {
		ref,
		reactive,
		onMounted
	} from 'vue'
	import {
		onPageScroll,
		onLoad,
		onShow,
		onReachBottom,
		onUnload
	} from "@dcloudio/uni-app";
	const statusBarHeight = ref('')
	const barHeight = ref('')
	const flag = ref(false)
	const pagetitle = ref('')
	const objnr = ref({})
	const back1 = ref('')
	const dose = ref('')
	const active = ref(2)
	const indicatorStyle = ref(`height: 40px`)
	const show = ref(false)
	const formData = reactive({
		plan_id: undefined,
		medicine_id: '',
		dose: '',
		usage_time: ''
	})
	const usage_timearr = ref([])
	onUnload(() => {
		uni.removeStorageSync('itemobj2')
	})
	onMounted(() => {
		statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight
		const {
			top,
			height
		} = wx.getMenuButtonBoundingClientRect();
		barHeight.value = height ? height + (top - statusBarHeight.value) * 2 : 38;
	})

	function bool(arr, num) {
		var ind = arr.findIndex(item => item == num)
		if (ind == -1) {
			return false
		} else {
			return true
		}
	}

	function xz(num) {
		var ind = usage_timearr.value.findIndex(item => item == num)
		if (ind == -1) {
			usage_timearr.value.push(num)
		} else {
			usage_timearr.value.splice(ind, 1)
		}
	}
	onShow(() => {
		let pagearr = getCurrentPages(); //获取应用页面栈
		let currentPage = pagearr[pagearr.length - 1]; //获取当前页面信息
		var opt = currentPage.options
		if (!opt.id) {
			pagetitle.value = '添加药品'
			uni.removeStorageSync('itemobj1')
		} else {
			pagetitle.value = '编辑药品'
			if (uni.getStorageSync('itemobj1')) {
				var obj1 = uni.getStorageSync('itemobj1')
				objnr.value = obj1
				formData['plan_id'] = obj1['plan_id']
				formData['medicine_id'] = obj1['medicine_id']
				formData['usage_time'] = obj1['usage_time']
				if (obj1['usage_time']) {
					usage_timearr.value = [obj1['usage_time']]
				} else {
					usage_timearr.value = []
				}
				formData['dose'] = obj1['dose']
			}

		}
		if (uni.getStorageSync('itemobj2')) {
			var obj1 = uni.getStorageSync('itemobj2')
			objnr.value = obj1
			formData['medicine_id'] = obj1['medicine_id']
			formData['usage_time'] = obj1['usage_time']
		}

	})

	async function save() {
		formData['usage_time'] = usage_timearr.value.join()
		for (var i in formData) {
			if (i != 'plan_id') {
				if (!formData[i]) {
					uni.showToast({
						title: '请补全后在提交',
						icon: 'none'
					})
					return
				}
			}
		}
		if (pagetitle.value == '编辑药品') {
			const res1 = await $api.editMedicinePlan(formData)
			if (res1.code == 1) {
				setTimeout(() => {
					uni.showToast({
						title: '编辑成功',
						icon: 'none'
					})
				}, 500)

				uni.navigateBack()
			} else {
				uni.showToast({
					title: res1.msg,
					icon: 'none'
				})
			}
			return
		}

		const res = await $api.addMedicineRecord(formData)
		if (res.code == 1) {
			setTimeout(() => {
				uni.showToast({
					title: '添加成功',
					icon: 'none'
				})
			}, 500)

			uni.navigateBack()
		}
	}

	function bindChange(e) {
		const val = e.detail.value
		dose.value = objnr.value['dose_list'][val[0]]
	}
	onPageScroll((e) => {
		if (e.scrollTop > 100) {
			back1.value = '#81D6FF'
		} else {
			back1.value = ''
		}
	})

	function open() {
		if (!formData['medicine_id']) {
			uni.showToast({
				title: '请选择药品',
				icon: 'none'
			})
			return
		}
		show.value = true
	}

	function queding() {
		if (!dose.value) {
			formData['dose'] = objnr.value['dose_list'][0]
		} else {
			formData['dose'] = dose.value
		}
		show.value = false
	}

	function gopage() {
		uni.navigateTo({
			url: '/sub_index/index/tjyp1'
		})
	}
</script>

<style lang="scss">
	.xzx {
		.back {
			background: #FFFFFF;
			width: 100%;
			overflow: hidden;
			border-radius: 30rpx 30rpx 0 0;

			.xuanxiang {
				width: 100%;
				display: flex;
				align-items: center;
				margin-top: 30rpx;
				overflow: hidden;
				justify-content: space-between;
				margin-bottom: 30rpx;
				background: #FFFFFF;

				.tt {
					font-size: 32rpx;
				}
			}

		}

		.picker-view {
			width: 100%;
			height: 600rpx;
			margin-top: 20rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 0 0;
		}

		.item {
			line-height: 100rpx;
			text-align: center;
		}

		::v-deep .u-popup__content {
			border-radius: 20rpx 20rpx 0 0;
		}
	}

	.fixed {
		width: 100%;
		height: 200rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -6rpx 14rpx 0rpx rgba(0, 0, 0, 0.03);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		overflow: hidden;
		position: fixed;
		bottom: 0%;

		.center {
			width: 686rpx;
			margin: auto;
			overflow: hidden;

			.savebtn {
				width: 686rpx;
				height: 82rpx;
				background: #2AB4F5;
				border-radius: 12rpx 12rpx 12rpx 12rpx;
				text-align: center;
				line-height: 82rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
				margin-top: 48rpx;
			}

			.xk {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #86909C;
				display: flex;
				align-items: center;

				image {
					width: 32rpx;
					height: 32rpx;
					margin-right: 16rpx;
				}
			}
		}
	}

	page {
		background: #F7F8FA;
	}

	.bianjiyaop {
		width: 100%;
		overflow: hidden;
		position: relative;

		.bianjiyaopback {
			width: 100%;
			height: 450rpx;
			background: linear-gradient(180deg, #CAEEFF 71%, rgba(240, 250, 255, 0) 100%);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			position: absolute;
			z-index: -1;
		}

		.mouldff {
			width: 686rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 4rpx 10rpx 0rpx rgba(0, 0, 0, 0.03);
			border-radius: 24rpx 24rpx 24rpx 24rpx;
			overflow: hidden;
			margin-top: 20rpx;
			padding: 8rpx 0;

			.line {
				width: 630rpx;
				height: 88rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				margin: auto;
				color: #1D2129;
				border-bottom: 2rpx solid #F2F3F5;

				image {
					width: 36rpx;
					height: 36rpx;
				}
			}
		}

		.centercontent {
			width: 686rpx;
			overflow: hidden;
			margin: auto;
			margin-top: 32rpx;

			.mouldff {
				width: 686rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 4rpx 10rpx 0rpx rgba(0, 0, 0, 0.03);
				border-radius: 24rpx 24rpx 24rpx 24rpx;
				overflow: hidden;
				margin-top: 20rpx;
				padding: 8rpx 0;

				.line {
					width: 630rpx;
					height: 88rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					margin: auto;
					color: #1D2129;
					border-bottom: 2rpx solid #F2F3F5;
				}
			}

			.msgtext {
				width: 100%;
				display: flex;
				align-items: center;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #0174AA;

				.gantanhao {
					width: 28rpx;
					height: 28rpx;
					margin-right: 12rpx;
				}
			}
		}
	}
</style>